const path = require('path');
const fs =  require('fs-extra');
const Inquirer = require('inquirer');
const chalk = require('chalk');
const Creator = require('./Creator');

module.exports = async (projectName, options) => {
    const pwd = process.env.PWD; // 命令执行所在目录
    console.log('pwd', pwd);
    const cwd = process.cwd(); // 执行文件所在目录
    console.log('cwd', cwd);
    const targetDir = path.join(cwd,projectName)
    if(fs.existsSync(targetDir)){
        if (options.force) {
            fs.remove(targetDir);
        } else {
            let { action } = await Inquirer.prompt([
                {
                    name: 'action',
                    type: 'list', // 类型非常丰富
                    message: 'target directory already exist please Pick an action',
                    choices: [
                        {name: 'Overwrite', value: 'overwrite'},
                        {name: 'Cance', value: 'cance'}
                    ]
                }
            ])
            if (action == 'cance') {
                return;
            } else {
                console.log(`\r\n${chalk.red('Removing...')}`);
                await fs.remove(targetDir)
            }
        }
    }
    /**
     * 创建项目
     * 1.先做一个命令交互功能
     * 2.将模板下载下来 download-git-repo
     * 3.根据用户的选项和模板合成 metasmith
     */
    console.log('generater a project dir');
    const creator = new Creator(projectName, targetDir);
    creator.create();
    
}